package cn.player.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Looper;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import cn.player.R;


/**
 * Created by DD_ZGL on 2016/4/11.
 */
public class TabChangedColorView extends View {

    private Bitmap mIconBitmap;
    private String mDefaultText;
    private String mText;
    private int mDefaultTargetTextColor;
    private int mTargetTextColor;
    private int mDefaultTextSize;
    private int mTextSize;

    private int mDefaultSourceTextColor;
    private float mDefaultSourceTextAlpha = 0.8f;

    private int mWidth;
    private int mHeight;
    private int mIconWidth;
    private int mIconHeight;
    private int mIconLeft;
    private int mIconTop;

    private Paint mTextPaint;

    //内存中绘制bitmap相关变量
    private Canvas mCanvas;
    private Bitmap mBitmap;
    private Paint mPaint;

    private float mAlpha;

    private Rect mIconRect;
    private Rect mTextRect;

    private int mPointCount;

    public TabChangedColorView(Context context) {
        super(context, null);
    }

    public TabChangedColorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttribute(context,attrs);
    }

    public TabChangedColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttribute(context,attrs);
    }

    private void initAttribute(Context context, AttributeSet attrs) {
        mDefaultText = "dd";
        mDefaultTargetTextColor = 0xff45c01a;//绿色
        mDefaultSourceTextColor = 0xff333333;//灰色
        mDefaultTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, context.getResources().getDisplayMetrics());

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TabChangedColorView);
        BitmapDrawable bitmapDrawable = (BitmapDrawable) a.getDrawable(R.styleable.TabChangedColorView_iconDrawable);
        mIconBitmap = bitmapDrawable.getBitmap();
        mText = a.getString(R.styleable.TabChangedColorView_text);
        mTargetTextColor = a.getColor(R.styleable.TabChangedColorView_textColor,mDefaultTargetTextColor);
        mTextSize = a.getDimensionPixelOffset(R.styleable.TabChangedColorView_textSize,mDefaultTextSize);

        a.recycle();

        mTextRect = new Rect();
        mTextPaint = new Paint();
        mTextPaint.setColor(mDefaultSourceTextColor);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.getTextBounds(mText,0,mText.length(),mTextRect);//相当于获取到了Text的布局数据，存入mTextRect


    }

    /**
     * 测量自己的宽和高，如果没有设置确切的值，会不断的被调用
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        /*int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);*/

        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();

        Log.i("zl","width = " + mWidth + ",height = " + mHeight);

        mIconWidth = Math.min(mWidth - getPaddingLeft() - getPaddingRight(),
                mHeight - getPaddingTop() - getPaddingBottom() - mTextRect.height());
        mIconHeight = mIconWidth;
//        mIconWidth = mIconBitmap.getWidth();
//        mIconHeight = mIconBitmap.getHeight();
        mIconLeft = mWidth / 2 - mIconWidth / 2;
        mIconTop = mHeight / 2 - (mIconWidth + mTextRect.height()) / 2;

        Log.i("zl","mIconWidth = " + mIconWidth  + ",mIconHeight = " + mIconHeight + ",mIconLeft = " + mIconLeft + ",mIconTop = " + mIconTop);
        mIconRect = new Rect(mIconLeft, mIconTop, mIconLeft + mIconWidth, mIconTop + mIconHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int alpha = (int) Math.ceil(255 * mAlpha);
        //画icon原图
        canvas.drawBitmap(mIconBitmap,null,mIconRect,null);
        //生成icon变色图mBitmap在内存中
        setTargetIconBitmap(alpha);
        //画icon变色图
        canvas.drawBitmap(mBitmap, 0, 0, null);

        drawSourceText(canvas,alpha);
        drawTargetText(canvas, alpha);

    }

    /**
     * draw目录icon，变色的图标
     * @param alpha alpha值
     *              0.0~1.0,图标颜色逐渐变深；
     *              1.0~0.0,图标颜色逐渐变淡
     */
    private void setTargetIconBitmap(int alpha) {
        //1.内存中创建纯色的背景bitmap
        mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);

        //2.设置paint属性并draw图标
        mPaint = new Paint();
        mPaint.setColor(mTargetTextColor);
        mPaint.setAntiAlias(true);//去锯齿
        mPaint.setDither(true);//防抖动
        mPaint.setFilterBitmap(true);//图像过滤
        mPaint.setAlpha(alpha);
        mCanvas.drawRect(mIconRect, mPaint);

        //3.设置paint Xfermode属性，重新draw图标，得到覆盖后的图标mBitmap
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
//        mPaint.setAlpha(255);
        mCanvas.drawBitmap(mIconBitmap, null, mIconRect, mPaint);
    }

    /**
     * draw目标色字体
     * @param canvas
     * @param alpha alpha值
     *              0.0~1.0,目标色逐渐变深；
     *              1.0~0.0,目标色逐渐变淡
     */
    private void drawTargetText(Canvas canvas, int alpha) {
        mTextPaint.setColor(mTargetTextColor);
        mTextPaint.setAlpha(alpha);
        mTextPaint.setAntiAlias(true);//去锯齿
        mTextPaint.setDither(true);//防抖动
        int x = mWidth/2 - mTextRect.width()/2;
        int y = mIconRect.bottom + mTextRect.height();
        canvas.drawText(mText,x,y,mTextPaint);//从文字的左下角画起
    }

    /**
     * draw灰色文字，
     * @param canvas
     * @param alpha alpha值
     *              0.0~1.0,灰色逐渐变淡；
     *              1.0~0.0,灰色逐渐变深
     */
    private void drawSourceText(Canvas canvas, int alpha) {
        mTextPaint.setColor(mDefaultSourceTextColor);
        mTextPaint.setAlpha(Math.min(255 - alpha, (int) (255 * mDefaultSourceTextAlpha)));
        mTextPaint.setAntiAlias(true);//去锯齿
        mTextPaint.setDither(true);//防抖动
        int x = mWidth/2 - mTextRect.width()/2;
        int y = mIconRect.bottom + mTextRect.height();
        canvas.drawText(mText,x,y,mTextPaint);//从文字的左下角画起
    }

    public void setPointCount(int count){
        mPointCount = count;
    }

    public void setIconAlpha(float alpha) {
        this.mAlpha = alpha;
        invalidateView();
    }

    private void invalidateView() {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            invalidate();
        } else {
            postInvalidate();
        }
    }

    private static final String INSTANCE_STATUS = "instance_status";
    private static final String STATUS_ALPHA = "status_alpha";

    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putParcelable(INSTANCE_STATUS, super.onSaveInstanceState());
        bundle.putFloat(STATUS_ALPHA, mAlpha);
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle) {
            Bundle bundle = (Bundle) state;
            mAlpha = bundle.getFloat(STATUS_ALPHA);
            super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATUS));
            return;
        }
        super.onRestoreInstanceState(state);
    }
}
